Skip to content

Use locale-aware sort for menu entries#34

Open
City-busz wants to merge 1 commit intogapan:masterfrom
City-busz:locale-sort
Open

Use locale-aware sort for menu entries#34
City-busz wants to merge 1 commit intogapan:masterfrom
City-busz:locale-sort

Conversation

@City-busz
Copy link
Copy Markdown

This fixes the problem that entries started with non-English characters are placed at the end of the list.

@gapan
Copy link
Copy Markdown
Owner

gapan commented Dec 15, 2025

I'm not sure what problem you're referring to. Can you provide some examples? I'm trying to run xdgmenumaker with different locales with and without your patch and I see no differences.

@jrd
Copy link
Copy Markdown

jrd commented Dec 15, 2025

If @City-busz could provide an automatic test along the commit that would help…

@City-busz
Copy link
Copy Markdown
Author

City-busz commented Dec 16, 2025

E.g. GNOME Clocks has the following name in Hungarian:

Name[hu]=Órák

Without this patch, when I run xdgmenumaker with LANG=hu_HU.UTF-8, I get the following order:

  • Időjárás
  • Lemezek
  • Számológép
  • Térképek
  • Órák

So the menu item for "Órák" is at the end of the list, which is not correct according to the Hungarian alphabet.

With this patch, I get the proper order:

  • Időjárás
  • Lemezek
  • Órák
  • Számológép
  • Térképek

@gapan
Copy link
Copy Markdown
Owner

gapan commented Dec 16, 2025

I tried that here, but Órák ends up at the end even with your patch. There is no difference with or without the patch.

@jrd
Copy link
Copy Markdown

jrd commented Dec 16, 2025

I made it work, but one thing is missing.

What I did:

First a simple test file locale-test.py:

import locale
from os import getenv

data = [
    "Időjárás",
    "Lemezek"
    "Számológép",
    "Térképek",
    "Órák",
]
locale.setlocale(locale.LC_ALL, getenv('LANG', ''))
print("simple sorted", sorted(data))
print("locale sorted", sorted(data, key=lambda entry: locale.strxfrm(entry)))
$ env LANG=C python locale-test.py
simple sorted ['Időjárás', 'LemezekSzámológép', 'Térképek', 'Órák']
locale sorted ['Időjárás', 'LemezekSzámológép', 'Térképek', 'Órák']

I installed the Hungarian locale, but I also get the correct result with my French locale (LANG=fr_FR.utf8)

$ echo $LANG
fr_FR.utf8
$ python locale-test.py
simple sorted ['Időjárás', 'LemezekSzámológép', 'Térképek', 'Órák']
locale sorted ['Időjárás', 'LemezekSzámológép', 'Órák', 'Térképek']
$ env LANG=hu_HU.UTF-8 python locale-test.py
simple sorted ['Időjárás', 'LemezekSzámológép', 'Térképek', 'Órák']
locale sorted ['Időjárás', 'LemezekSzámológép', 'Órák', 'Térképek']

I also got this in Greek Gapan, so you can probably test invoking python directly:

$ env LANG=el_GR.UTF-8 python locale-test.py
simple sorted ['Időjárás', 'LemezekSzámológép', 'Térképek', 'Órák']
locale sorted ['Időjárás', 'LemezekSzámológép', 'Órák', 'Térképek']

The important thing is to do locale.setlocale(locale.LC_ALL, getenv('LANG', '')) at the start of the application.

I just discovered that last week for another python project. Using locale.setlocale(locale.LC_ALL, '') is not enough.

For this particular thing (only sorting) then locale.setlocale(locale.LC_COLLATE, '') is enough because only the collation (the way of sorting) is important here.

https://docs.python.org/3/library/locale.html#locale.setlocale

@jrd
Copy link
Copy Markdown

jrd commented Dec 16, 2025

So please @City-busz add locale.setlocale(locale.LC_COLLATE, '') just before calling main or at the start of main in your commit.

This fixes the problem that entries started with non-English characters
are placed at the end of the list.
@City-busz
Copy link
Copy Markdown
Author

Okay, I added locale.setlocale(locale.LC_COLLATE, ''). Thanks for the tip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants